package xunmeng.util;

import com.alibaba.druid.pool.DruidAbstractDataSource;
import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.pool.DruidDataSourceFactory;

import javax.sql.DataSource;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Properties;

/**
 * JDBC工具类，用Bruid连接池
 */
public class JDBCUitls {
    private static DataSource ds;
    //获取数据库连接
    static {
        try {
            //1.加载配置文件
            Properties properties=new Properties();
            //使用ClassLoader加载配置文件获取输入流
            InputStream inputStream=JDBCUitls.class.getClassLoader().getResourceAsStream("druid.properties");
            //加载配置文件输入流
            properties.load(inputStream);
            //2.初始化连接池对象
            ds= DruidDataSourceFactory.createDataSource(properties);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
    //获取连接池对象
    public static DataSource getDataSource(){
        return ds;
    }
    //获取连接对象
    public static Connection getConnection() throws SQLException {
        return ds.getConnection();
    }
}
